package com.vip.iot.util;

import com.vip.iot.entity.Vector3D;
import org.apache.commons.lang3.StringUtils;

/**
 * 通用工具类
 *
 * @author wgb
 * @Date 2019/3/12
 */
public class CommonUtils {

    /**
     * 将unicode码转化成字符串
     * @param unicode
     * @return
     * @author shuai.ding
     */
    public static String unicodeToString(String unicode) {
        if (StringUtils.isBlank(unicode)) {
            return null;
        }

        StringBuilder sb = new StringBuilder();
        int i = -1;
        int pos = 0;

        while ((i = unicode.indexOf("\\u", pos)) != -1) {
            sb.append(unicode.substring(pos, i));
            if (i + 5 < unicode.length()) {
                pos = i + 6;
                sb.append((char) Integer.parseInt(unicode.substring(i + 2, i + 6), 16));
            }
        }
        //如果pos位置后，有非中文字符，直接添加
        sb.append(unicode.substring(pos));
        return sb.toString();
    }


    /**
     * 经纬度转墨卡托坐标
     * @param lonLat
     * @return
     */
    public static Vector3D lonLat2Mercator(Vector3D lonLat) {
        Vector3D mercator = new Vector3D();
        double x = lonLat.getX() * 20037508.34 / 180;
        double y = Math.log(Math.tan((90 + lonLat.getY()) * Math.PI / 360)) / (Math.PI / 180);
        y = y * 20037508.34 / 180;
        mercator.setX(x);
        mercator.setY(y);
        mercator.setZ(lonLat.getZ());
        return mercator;
    }

    /**
     * 墨卡托坐标转经纬度
     * @param mercator
     * @return
     */
    public static Vector3D Mercator2lonLat(Vector3D mercator) {
        Vector3D lonLat = new Vector3D();
        double x = mercator.getX() / 20037508.34 * 180;
        double y = mercator.getY() / 20037508.34 * 180;
        y = 180 / Math.PI * (2 * Math.atan(Math.exp(y * Math.PI / 180)) - Math.PI / 2);
        lonLat.setX(x);
        lonLat.setY(y);
        lonLat.setZ(mercator.getZ());
        return lonLat;
    }
}
